package com.school_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school_admin.config.Result;
import com.school_admin.entity.Operation;
import com.school_admin.entity.Post;
import com.school_admin.entity.User;
import com.school_admin.mapper.PostMapper;
import com.school_admin.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-03-18
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
    @Autowired
    private OperationServiceImpl operationService;
    @Autowired
    private PostMapper postMapper;

    @Override
    public Result<?> getPostListWeb(int page, int pageSize, String uid) {
//         判断该用户使用存在文章操作行为
//        List<Operation> operation = operationService.getUserNum(uid);
        String tags = postMapper.getUsertags(uid);
        if (tags != null && !tags.equals("")) {
            // 转成数组
            String[] tagArr = tags.split(",");
            // 随机抽取标签
            String tag = tagArr[(int) (Math.random() * tagArr.length)];
            // 获取该标签下的文章
            int start = (page - 1) * pageSize;
            List<Map<String, Object>> postList = postMapper.getPostListWebByTag(start, pageSize, tag);
            Map<String, Object> map = new HashMap<>();
            // TODO 如果用户有设置标签 根据喜好标签进行推荐
            if (postList.size() == 0) {
                // 随机推荐
                List<Map<String, Object>> postList1 = postMapper.random(start, pageSize);
                map.put("postList", postList1);
                map.put("total", postMapper.getPostListWebCount());
                return Result.success(map);
            }
            // 按照标签推荐
            map.put("postList", postList);
            map.put("total", postMapper.getPostListTagCount());
            return Result.success(tags);
        } else {
            // 常规获取
            int start = (page - 1) * pageSize;
            List<Map<String, Object>> postList = postMapper.getPostListWeb(start, pageSize);
            Map<String, Object> map = new HashMap<>();
            map.put("postList", postList);
            map.put("total", postMapper.getPostListWebCount());
            return Result.success(map);
        }
    }

    @Override
    public Result<?> queryPost(String postId) {
        List<Map<String, Object>> postObj = postMapper.queryPost(postId);
        if (postObj.size() > 0) {
            return Result.success(postObj.get(0));
        }
        return Result.fail("不存在该文章");
    }

    @Override
    public Result<?> getPostListAdmin(int page, int pageSize) {
        int start = (page - 1) * pageSize;
        List<Map<String, Object>> postList = postMapper.getPostListAdmin(start, pageSize);
        if (postList.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", postList);
            map.put("total", postMapper.getPostListAdminCount());
            return Result.success(map);
        }
        return Result.fail("帖子列表为空");
    }

    // 搜索
    @Override
    public Result<?> getPostListAdminSearch(int page, int pageSize, String keyWord) {
        int start = (page - 1) * pageSize;
        List<Map<String, Object>> list = postMapper.getSearchListAdmin(keyWord, start, pageSize);
        if (list.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("total", postMapper.getSearchListAdminCount(keyWord));
            return Result.success(map);
        }
        return Result.fail("搜索结果为空");
    }
}
